home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / ppmrelie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  2.7 KB  |  96 lines

  1. /* ppmrelief.c - generate a relief map of a portable pixmap
  2. **
  3. ** Copyright (C) 1990 by Wilson H. Bent, Jr.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "ppm.h"
  15.  
  16. void
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     pixel** inputbuf;
  23.     pixel* outputrow;
  24.     int argn, rows, cols, format, row, rowa, rowb;
  25.     register int col;
  26.     pixval maxval, mv2, r, g, b;
  27.     char* usage = "[ppmfile]";
  28.  
  29.     ppm_init( &argc, argv );
  30.  
  31.     argn = 1;
  32.  
  33.     if ( argn != argc )
  34.     {
  35.     ifp = pm_openr( argv[argn] );
  36.     ++argn;
  37.     }
  38.     else
  39.     ifp = stdin;
  40.  
  41.     if ( argn != argc )
  42.     pm_usage( usage );
  43.  
  44.     ppm_pbmmaxval = PPM_MAXMAXVAL;    /* use larger value for better results */
  45.  
  46.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  47.     mv2 = maxval / 2;
  48.  
  49.     /* Allocate space for 3 input rows, plus an output row. */
  50.     inputbuf = ppm_allocarray( cols, 3 );
  51.     outputrow = ppm_allocrow( cols );
  52.  
  53.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  54.  
  55.     /* Read in the first two rows. */
  56.     ppm_readppmrow( ifp, inputbuf[0], cols, maxval, format );
  57.     ppm_readppmrow( ifp, inputbuf[1], cols, maxval, format );
  58.  
  59.     /* Write out the first row, all zeros. */
  60.     for ( col = 0; col < cols; ++col )
  61.         PPM_ASSIGN( outputrow[col], 0, 0, 0 );
  62.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  63.  
  64.     /* Now the rest of the image - read in the 3rd row of inputbuf,
  65.     ** and convolve with the first row into the output buffer.
  66.     */
  67.     for ( row = 2 ; row < rows; ++row )
  68.     {
  69.     rowa = row % 3;
  70.     rowb = (row + 2) % 3;
  71.     ppm_readppmrow( ifp, inputbuf[rowa], cols, maxval, format );
  72.  
  73.     for ( col = 0; col < cols - 2; ++col )
  74.         {
  75.         r = PPM_GETR( inputbuf[rowa][col] ) +
  76.         ( mv2 - PPM_GETR( inputbuf[rowb][col + 2] ) );
  77.         g = PPM_GETG( inputbuf[rowa][col] ) +
  78.         ( mv2 - PPM_GETG( inputbuf[rowb][col + 2] ) );
  79.         b = PPM_GETB( inputbuf[rowa][col] ) +
  80.         ( mv2 - PPM_GETB( inputbuf[rowb][col + 2] ) );
  81.         PPM_ASSIGN( outputrow[col + 1], r, g, b );
  82.         }
  83.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  84.     }
  85.  
  86.     /* And write the last row, zeros again. */
  87.     for ( col = 0; col < cols; ++col )
  88.         PPM_ASSIGN( outputrow[col], 0, 0, 0 );
  89.     ppm_writeppmrow( stdout, outputrow, cols, maxval, 0 );
  90.  
  91.     pm_close( ifp );
  92.     pm_close( stdout );
  93.  
  94.     exit( 0 );
  95.     }
  96.